package com.example.clipboardsyncapp

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.example.clipboardsyncapp.data.api.ClipboardApi
import com.example.clipboardsyncapp.ui.screen.MainScreen
import com.example.clipboardsyncapp.ui.theme.ClipboardSyncAppTheme
import com.example.clipboardsyncapp.ui.viewmodel.MainViewModel
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

class MainActivity : ComponentActivity() {
    private val okHttpClient by lazy {
        OkHttpClient.Builder()
            .followRedirects(true)
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build()
    }

    private val clipboardApi by lazy {
        ClipboardApi(okHttpClient, applicationContext)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val viewModelFactory = viewModelFactory {
            initializer {
                MainViewModel(clipboardApi, applicationContext)
            }
        }

        setContent {
            ClipboardSyncAppTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val viewModel = ViewModelProvider(this, viewModelFactory)[MainViewModel::class.java]
                    MainScreen(viewModel)
                }
            }
        }
    }
}